using System;
using UnityEngine;

public class WeaponCartridge : ScriptableObject
{
	public static bool effectsHitIsEnabled = true;

	private static int idTriggers = 0;

	public static EntityTriggers[] entities = new EntityTriggers[0];

	private static EntityTriggers[] tempList = new EntityTriggers[0];

	public WeaponSourceBullets sourceBullets = new WeaponSourceBullets(1, 2f, 120f);

	public SoundHitBullet[] soundsHit;

	public WeaponEffectsHits effectsHits;

	private float angleRad;

	private HitBullet[] bulletHits = new HitBullet[0];

	private Ray rayGizmos = default(Ray);

	private float distLastAim;

	private ShotBullet shotBullet = default(ShotBullet);

	private HitBullet hitBullet = default(HitBullet);

	private RaycastHit2D[] rayHits = new RaycastHit2D[0];

	private Collider2D[] collidersOverlap = new Collider2D[0];

	private RaycastHit2D raycastHit;

	private IWeaponCartridgeEntity entity;

	private float timePlaySound;

	public static void ActivateTriggers(int idShot)
	{
		if (idTriggers == 0)
		{
			idTriggers = idShot;
			for (int i = 0; i < entities.Length && entities[i] != null; i++)
			{
				entities[i].SetActiveTriggers(value: true);
			}
		}
	}

	public static void DeactivateTriggers(int idShot)
	{
		if (idTriggers == idShot)
		{
			idTriggers = 0;
			for (int i = 0; i < entities.Length && entities[i] != null; i++)
			{
				entities[i].SetActiveTriggers(value: false);
			}
		}
	}

	public static void AddEntityTriggers(IWeaponCartridgeEntity entity)
	{
		if (entities.Length == 0 || entities[0] == null)
		{
			idTriggers = 0;
		}
		for (int i = 0; i < entities.Length; i++)
		{
			if (entities[i] == null)
			{
				entities[i] = new EntityTriggers(entity);
				entities[i].SetActiveTriggers(idTriggers != 0);
				return;
			}
		}
		tempList = entities;
		entities = new EntityTriggers[tempList.Length + 1];
		for (int j = 0; j < tempList.Length; j++)
		{
			entities[j] = tempList[j];
		}
		entities[tempList.Length] = new EntityTriggers(entity);
		entities[tempList.Length].SetActiveTriggers(idTriggers != 0);
		tempList = new EntityTriggers[0];
	}

	public static void UpdateEntityTriggers(IWeaponCartridgeEntity entity)
	{
		int num = 0;
		while (true)
		{
			if (num < entities.Length && entities[num] != null)
			{
				if (entities[num].entity == entity)
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		entities[num].UpdateTriggers();
	}

	public static void RemoveEntityTriggers(IWeaponCartridgeEntity entity)
	{
		int num = 0;
		while (true)
		{
			if (num < entities.Length && entities[num] != null)
			{
				if (entities[num].entity == entity)
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		entities[num] = null;
		entity = null;
		for (int i = num; i < entities.Length - 1; i++)
		{
			entities[i] = entities[i + 1];
			entities[i + 1] = null;
		}
	}

	public static IWeaponCartridgeEntity GetEntity(Collider2D trigger)
	{
		for (int i = 0; i < entities.Length && entities[i] != null; i++)
		{
			for (int j = 0; j < entities[i].colliders.Length; j++)
			{
				if (entities[i].colliders[j] == trigger)
				{
					return entities[i].entity;
				}
			}
		}
		return null;
	}

	public virtual void AwakeWeapon(WeaponHands weapon)
	{
		if (effectsHits != null)
		{
			effectsHits.AwakeWeapon();
		}
	}

	public virtual void OnDrawGizmosWeapon(WeaponHands weapon)
	{
	}

	public virtual void OnDrawGizmosSelectedWeapon(WeaponHands weapon)
	{
		Vector2 vector = default(Vector2);
		float num = 0f;
		Vector3 direction = rayGizmos.direction;
		if (direction.x != 0f)
		{
			Gizmos.color = new Color(1f, 0.4f, 0f);
			Gizmos.DrawLine(rayGizmos.origin, rayGizmos.origin + rayGizmos.direction * 20f);
			Gizmos.color = new Color(1f, 0.9f, 0f);
			Gizmos.DrawWireSphere(rayGizmos.origin + rayGizmos.direction * distLastAim, 0.16f);
		}
		if (bulletHits != null && bulletHits.Length != 0)
		{
			Gizmos.color = new Color(1f, 0.4f, 0f);
			for (int i = 0; i < bulletHits.Length; i++)
			{
				Gizmos.DrawWireSphere(bulletHits[i].raycastHit.point, 0.2f);
				Gizmos.DrawLine(bulletHits[i].raycastHit.point, bulletHits[i].raycastHit.point + bulletHits[i].raycastHit.normal);
			}
		}
		Gizmos.color = new Color(1f, 0.2f, 0f);
		vector = weapon.transform.TransformPoint(weapon.bolt.x, weapon.bolt.y, 0f);
		float z = weapon.bolt.z;
		Vector3 eulerAngles = weapon.transform.rotation.eulerAngles;
		float num2 = z + eulerAngles.z;
		angleRad = num2 * ((float)Math.PI / 180f);
		Vector2 b = new Vector2(Mathf.Cos(angleRad) * sourceBullets.distance, Mathf.Sin(angleRad) * sourceBullets.distance);
		if (sourceBullets.distance <= 0f)
		{
			Gizmos.DrawLine(vector, weapon.transform.TransformPoint(weapon.bolt.x + 3f, weapon.bolt.y, 0f));
		}
		else
		{
			Gizmos.DrawLine(vector, vector + b);
			Gizmos.DrawWireSphere(vector + b, 0.2f);
			Gizmos.color = new Color(1f, 0.5f, 0f);
		}
		num = sourceBullets.angleScatter * weapon.scaleAngleScatter;
		if (num != 0f)
		{
			Gizmos.color = new Color(1f, 0.5f, 0f);
			angleRad -= num * ((float)Math.PI / 180f);
			float num3 = 30f;
			if (sourceBullets.distance != 0f)
			{
				num3 = sourceBullets.distance;
			}
			Gizmos.DrawLine(vector, new Vector2(vector.x + Mathf.Cos(angleRad) * num3, vector.y + Mathf.Sin(angleRad) * num3));
			angleRad += num * ((float)Math.PI / 180f) * 2f;
			Gizmos.DrawLine(vector, new Vector2(vector.x + Mathf.Cos(angleRad) * num3, vector.y + Mathf.Sin(angleRad) * num3));
			float num4 = 0f;
			float num5 = 0f;
			while (sourceBullets.bullets > 1 && num5 < (float)sourceBullets.bullets)
			{
				num4 = GetAngle(num2, num, sourceBullets.evenlySpread, num5, sourceBullets.bullets);
				Gizmos.DrawLine(vector, new Vector2(vector.x + Mathf.Cos(num4) * num3, vector.y + Mathf.Sin(num4) * num3));
				num5 += 1f;
			}
		}
	}

	public float GetAngle(float globalAngleWeapon, float angleScatter, float evenlySpread, float currentArrow, float totalArrows)
	{
		float a = angleScatter - angleScatter * 2f * UnityEngine.Random.value;
		float num = angleScatter - angleScatter * 2f * (currentArrow / totalArrows);
		num -= angleScatter / totalArrows;
		float num2 = Mathf.LerpAngle(a, num, evenlySpread);
		num2 = globalAngleWeapon + num2;
		return num2 * ((float)Math.PI / 180f);
	}

	public virtual void StartWeapon(WeaponHands weapon)
	{
	}

	public virtual void Shot(WeaponHands weapon, float distToAim)
	{
	}

	public virtual void OnHitBullet(HitBullet hit)
	{
	}

	public virtual float GetScaleScoresBullet()
	{
		return 1f / (float)sourceBullets.bullets;
	}

	public void ShotBullet(WeaponSourceBullets bullet, WeaponHands weapon, Vector2 rayPosition, float globalAngle, float distToAim)
	{
		ActivateTriggers(weapon.GetInstanceID());
		bullet.angleScatter *= weapon.scaleAngleScatter;
		distLastAim = distToAim;
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		bullet.hitsBullet.numsMax = Mathf.Max(1, bullet.hitsBullet.numsMax);
		collidersOverlap = Physics2D.OverlapPointAll(rayPosition, bullet.layerBodies.value);
		for (float num4 = 0f; num4 < (float)bullet.bullets; num4 += 1f)
		{
			shotBullet = new ShotBullet(bullet.hitsBullet.GetRandomNumHits(), rayPosition, GetAngle(globalAngle, bullet.angleScatter, bullet.evenlySpread, num4, bullet.bullets));
			rayHits = Physics2D.RaycastAll(rayPosition, shotBullet.rayDirection, bullet.distance, bullet.layerBodies.value);
			for (int i = 0; i < collidersOverlap.Length; i++)
			{
				for (num2 = 0; num2 < rayHits.Length - 1; num2++)
				{
					if (rayHits[num2].collider == collidersOverlap[i])
					{
						rayHits[num2] = rayHits[num2 + 1];
					}
				}
			}
			int num5 = 0;
			for (num2 = 0; num2 < rayHits.Length && rayHits[num2].distance <= distToAim; num2++)
			{
				entity = GetEntity(rayHits[num2].collider);
				if (entity != null && entity.IsHighPriorityBullet())
				{
					raycastHit = rayHits[num5];
					rayHits[num5] = rayHits[num2];
					rayHits[num2] = raycastHit;
					num5++;
				}
			}
			num5 = 0;
			for (num2 = 0; num2 < rayHits.Length - num5; num2++)
			{
				if (GetEntity(rayHits[num2].collider) == null)
				{
					raycastHit = rayHits[num2];
					for (num = num2; num < rayHits.Length - 1; num++)
					{
						rayHits[num] = rayHits[num + 1];
					}
					rayHits[rayHits.Length - 1] = raycastHit;
					num5++;
					num2 = 0;
				}
			}
			if (rayHits.Length >= 2 && UnityEngine.Random.value <= 0.5f)
			{
				entity = GetEntity(rayHits[0].collider);
				if (entity != null && entity == GetEntity(rayHits[1].collider) && rayHits[0].collider.offset == rayHits[1].collider.offset && rayHits[0].collider.gameObject != rayHits[1].collider.gameObject && rayHits[0].collider.sharedMaterial == rayHits[1].collider.sharedMaterial)
				{
					raycastHit = rayHits[0];
					rayHits[0] = rayHits[1];
					rayHits[1] = raycastHit;
				}
			}
			for (num2 = num3 + 1; num2 < rayHits.Length; num2++)
			{
				if (!bullet.hitsBullet.IsHitRangeDist(rayHits[num3].distance, rayHits[num2].distance))
				{
					rayHits[num2] = default(RaycastHit2D);
				}
				else if (rayHits[num3].collider.gameObject == rayHits[num2].collider.gameObject || rayHits[num3].collider.transform == rayHits[num2].collider.transform.parent || rayHits[num3].collider.transform.parent == rayHits[num2].collider.transform)
				{
					rayHits[num2] = default(RaycastHit2D);
				}
				else
				{
					num3 = num2;
				}
			}
			num3 = 0;
			if (rayHits.Length != 0 && (bullet.hitsBullet.modeHit == HitsBullet.ModeHit.RANDOM_ONCE || bullet.hitsBullet.modeHit == HitsBullet.ModeHit.RANDOM_FIRST))
			{
				num3 = UnityEngine.Random.Range(0, rayHits.Length);
				if (bullet.hitsBullet.modeHit == HitsBullet.ModeHit.RANDOM_FIRST && rayHits[num3].collider == null)
				{
					num3 = 0;
				}
			}
			for (num2 = num3; num2 < rayHits.Length; num2++)
			{
				if (rayHits[num2].collider != null && rayHits[num2].collider.sharedMaterial != null)
				{
					hitBullet = new HitBullet(this, shotBullet, weapon, bullet, rayHits[num2]);
					weapon.OnHitBullet(hitBullet);
					OnHitBullet(hitBullet);
					PlaySoundHit(weapon, hitBullet.raycastHit.collider.sharedMaterial.name);
					if (effectsHits != null)
					{
						effectsHits.OnHitBullet(hitBullet);
					}
					if (hitBullet.entity != null && (string.IsNullOrEmpty(hitBullet.cartridgeRay.tagEntity) || hitBullet.entity.CompareTag(hitBullet.cartridgeRay.tagEntity)))
					{
						hitBullet.entity.OnHitBullet(hitBullet);
					}
					if (bullet.hitsBullet.modeHit == HitsBullet.ModeHit.RANDOM_ONCE || bullet.hitsBullet.modeHit == HitsBullet.ModeHit.RANDOM_FIRST || hitBullet.StopBullet())
					{
						break;
					}
				}
			}
		}
		DeactivateTriggers(weapon.GetInstanceID());
		rayGizmos = new Ray(rayPosition, shotBullet.rayDirection);
		collidersOverlap = null;
		entity = null;
	}

	public void ShotBullet(WeaponSourceBullets bullet, WeaponHands weapon, float distToAim)
	{
		ShotBullet(bullet, weapon, weapon.GetPositionBolt(), weapon.GetAngleBolt(), distToAim);
	}

	public void PlaySoundHit(WeaponHands weapon, string sharedMaterialName)
	{
		if (timePlaySound == Mathf.Floor(Time.time * 1000f) || soundsHit.Length == 0 || !(weapon.audioSource != null))
		{
			return;
		}
		timePlaySound = Mathf.Floor(Time.time * 1000f);
		if (soundsHit.Length == 1 && soundsHit[0].material.name == sharedMaterialName)
		{
			weapon.audioSource.PlayOneShot(soundsHit[0].sound);
		}
		else
		{
			if (soundsHit.Length <= 1)
			{
				return;
			}
			int num = 0;
			while (true)
			{
				if (num < soundsHit.Length)
				{
					if (soundsHit[num].material.name == sharedMaterialName)
					{
						break;
					}
					num++;
					continue;
				}
				return;
			}
			weapon.audioSource.PlayOneShot(soundsHit[num].sound);
		}
	}
}
